package com.io.nio.mappedByteBuffer;

import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;

public class Demo2 {

	public static void main(String[] args) throws IOException {
		String srcFile = "nio.txt";
		String destFile = "niocopy.txt";
		RandomAccessFile rafi = new RandomAccessFile(srcFile, "r");
		RandomAccessFile rafo = new RandomAccessFile(destFile, "rw");
		FileChannel fci = rafi.getChannel();
		FileChannel fco = rafo.getChannel();
		long size = fci.size();
		MappedByteBuffer mbbi = fci.map(FileChannel.MapMode.READ_ONLY, 0, size);
		MappedByteBuffer mbbo = fco
				.map(FileChannel.MapMode.READ_WRITE, 0, size);
		long start = System.currentTimeMillis();
		for (int i = 0; i < size; i++) {
			byte b = mbbi.get(i);
			mbbo.put(i, b);
		}
		fci.close();
		fco.close();
		rafi.close();
		rafo.close();
		System.out.println("Spend: "
				+ (double) (System.currentTimeMillis() - start) / 1000 + "s");
	}

}
